<?php

namespace app\api\controller\common;

use app\common\controller\IndexBase;
use app\common\helper\WxHelper;
use app\common\logic\common\Area as areaLogic;
use app\common\model\Archives;
use app\common\model\Attachment;
use app\common\model\Banner;
use app\common\model\Notice;
use app\common\model\User;
use think\captcha\facade\Captcha;
use think\Exception;
use think\helper\Str;

class Common extends IndexBase
{
    public function upload()
    {
        try {
            if (!isset($_FILES['file'])) {
                return $this->error("请选择文件");
            }
            $file = $_FILES['file'];
            if ($file["size"] > 1024*1024*5) { // 5M
                return $this->error("文件不能大于5M");
            }
            $type = safeInput('type','card');
            $ext = strtolower(strrchr($file["name"],"."));
            $path = "uploads/" . $type . "/" . date('Ym');
            if (!is_dir($path)) {
                mkdir($path,0777,true);
            }
            $name = md5(time() . Str::random(6)) . $ext;
            $name = $path . "/" . $name;
            $allows = ".jpg,.bmp,.gif,.png,.webp,.jpeg,.zip,.rar,.pdf,.xls,.xlsx,.gz,.doc,.docx,.mp3,.mp4";
            $allows = explode(",",$allows);
            if (!in_array($ext,$allows)) {
                return $this->error("不允许上传的文件类型。");
            }

            if (!move_uploaded_file($file["tmp_name"],$name)) {
                return $this->error("上传失败");
            }
            Attachment::create([
                "path" => "/".$name,
                "name" => $file["name"],
                "size" => $file["size"],
                "type" => $file["type"],
                "ext" => $ext
            ]);
            $url = $this->request->domain() . "/" . $name;
            $this->success(["url"=>$url,"path"=>"/".$name,"name"=>$file["name"]]);
        } catch (Exception $exception) {
            return  $this->error($exception->getMessage());
        }
    }

    public function getAreaList()
    {
        $this->success(areaLogic::getCityTree());
    }

    public function getMobile()
    {
        $phone = (new WxHelper())->getPhoneNumber($this->data['code']);
        $this->success(['tel'=>$phone]);
    }


    public function getConfig()
    {
        $key = input('key');
        $list = cache('config');
        $result = [];
        foreach (explode(",",$key) as $key) {
            if ($key == "bg_image") {
                $result['bg'] = $this->request->domain() . $list[$key];
            } else {
                $result[$key] = $list[$key];
            }
        }
        $this->success($result);
    }

    public function delFile()
    {
        $path = input("path");
        delFile($path);
        $this->success();
    }

    // 获取轮播图
    public function getBannerList()
    {
        $flag = input('flag','wx');
        $list = Banner::where('status',2)
            ->where('flag',$flag)
            ->order('sort desc')->select();
        $this->success($list);
    }
    
    // 获取通知
    public function getNoticeList()
    {
        $list = Notice::order('sort desc')
            ->limit(5)
            ->select();
        $this->success($list);
    }
    
    public function getArticle()
    {
        $config = cache('config');
        $temp = [
            "about" => str_replace("/uploads/",request()->domain()."/uploads/",$config['jianjie']),
            "service" => str_replace("/uploads/",request()->domain()."/uploads/",$config['service']),
            "privacy" => str_replace("/uploads/",request()->domain()."/uploads/",$config['privacy']),
        ];
        $this->success($temp);
    }

    // 获取权益，已分享的人数
    public function getShareInfo()
    {
        $article = Archives::where("id",19)->find();
        $token = $this->request->header('token');
        $num = 0;
        if ($token) {
            $user = User::where("token",$token)->find();
            if ($user) {
                $num = User::where("pid",$user->id)->count();
            }
        }
        $this->success([
            "info" => $article,
            "num" => $num
        ]);
    }
}